import { getStudents, getAllStudents } from '../../../services/student'
export const actionTypes = {
    // 设置学生查询结果数组和总数
    setStudents: Symbol('setStudentTotal'),
    loading: Symbol('isloading'),
    fetchStudents: Symbol('fetchStudents'),
}


/** 
 * 根据新的查询数据，产生一个action
 * @param {object} payload {students:[], total:0}
 */
export const createStudentAction = (payload) => ({
    type: actionTypes.setStudents,
    payload
})

/**
 * 根据当前仓库的查询条件，查询学生数据
 * @return {*} 
 */
export const fetchStudents = function () {
    return async function (dispacth, getState) {
        const condition = getState().student.condition // 查询条件
        dispacth(createLoadingAction(true))
        const stus = await getAllStudents()
        dispacth(createStudentAction({ students: stus, total: stus.length }))
        dispacth(createLoadingAction(false))
    }
}

/** 
 * 是否查询中action
 * @param {boolean} payload
 */
export const createLoadingAction = (payload) => ({
    type: actionTypes.loading,
    payload
})


export const fetchStudents2 = () => ({ type: actionTypes.fetchStudents })